import Cookies from 'js-cookie'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus')
    },
    socket: null, // soket链接成功后的对象
    manConf: { // 状态管理-手动配置
      WS_API: process.env.WS_API // webscoket 地址
    }
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
    },
    WEB_SOCKET: (state, socket) => {
      state.socket = socket
    },
    MAN_CONF: (state, arr) => {
      state.manConf[arr[0]] = arr[1]
    },
    ALL_CONF: (state, obj) => { // 所有配置-url覆盖配置
      state.manConf = Object.assign(state.manConf, obj)
    },
    SET_CID: (state, val) => { // 设置-渠道ID
      state.manConf['CHANNEL_ID'] = val
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    setWebSocket: ({ commit }, socket) => {
      commit('WEB_SOCKET', socket)
    },
    setManConf: ({ commit }, arr) => {
      commit('MAN_CONF', arr)
    },
    setAllConf: ({ commit }, obj) => {
      commit('ALL_CONF', obj)
    }
  }
}

export default app
